package com.ludomancy.hugealien;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.Vector;

import com.ludomancy.gamelib.ImageStrip;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.gamelib.Sound;
import com.ludomancy.gamelib.Utils;

public class Alien extends BasicEntity
{
	public int directionX = 0;
	public int directionY = 0;
	private ImageStrip alienImg;
	private boolean cutNow;
	private boolean alreadyCut = false;
	
	// moon state
	private float happyMoon = 1.0f;
	private float deathMoon = 1.0f;
	private float regularMoon = 0.0f;
	private ImageStrip moon;
	private Point fixed;
	public String finaleMessage = "";
	
	public Alien()
	{
		alienImg = new ImageStrip(ResourceManager.getImage("alien.png"), 19, 17);
		moon = new ImageStrip(ResourceManager.getImage("moon.png"), 23, 23);
	}
	
	public Rectangle area()
	{
		int w = alienImg.frameWidth;
		int h = alienImg.frameHeight;
		return new Rectangle(x,y, w, h);
	}
	
	public Point getHookPoint()
	{
		return new Point(x+14, y+12);
	}
	
	public Rectangle scissorArea()
	{
		return new Rectangle(x+11,y+5, 6, 6);
	}

	public void update()
	{
		if(!Game.game.isOver)
		{
	
			Rectangle area = area();
			
			// snap to playfield
			if(area.x < 0)
				x = area.width/2;
			if(area.y < 0)
				y = 0;
			if(area.x + area.width > Game.WORLD_WIDTH)
				x = Game.WORLD_WIDTH - area.width;
			if(area.y + area.height > Game.WORLD_HEIGHT)
				y = Game.WORLD_HEIGHT - area.height;
			
			if(cutNow && !alreadyCut)
			{
				// cut relationships with the scissors
				Vector rels = Game.game.city.getRelationshipsInArea(scissorArea());
				for (int i = 0; i < rels.size(); i++)
				{
					Uruguayan[] rel = (Uruguayan[]) rels.elementAt(i);
					// cut them all!
					if(Game.game.soulWorld.canCut(rel[0].soul, rel[1].soul))
					{
						// create a star representing the relationship
						Game.game.city.addStar(new Star(rel[0].soul, rel[1].soul, Star.TYPE_GRAY));
						Game.game.soulWorld.cutRelationship(rel[0].soul, rel[1].soul);
					}
				}
				if(!rels.isEmpty())
				{
					Sound.play("rip.wav");
				}
				alreadyCut = true;
			}
			// is the game over?
			// - no living uruguayans
			// - all new relationships
			// - everybody is depressed
			int liveUruguayans = 0;
			boolean allNewRelationships = true;
			int depressed = 0;
			for (int i = 0; i < Game.game.city.entities.size(); i++)
			{
				BasicEntity be = (BasicEntity) Game.game.city.entities.elementAt(i);
				if(be instanceof Uruguayan)
				{
					Uruguayan u = (Uruguayan) be;
					liveUruguayans++;
					if(u.soul.hasInitialRelationships())
					{
						allNewRelationships = false;
					}
					if(u.soul.getMood() == Soul.MOOD_HEARTBROKEN)
					{
						depressed++;
					}
				}
			}
			
			if(!Game.game.soulWorld.isThereAShockedSoul())
			{
				if(liveUruguayans <= 6 || depressed == liveUruguayans)
				{
					Game.game.endGame();
					happyMoon = 0.0f;
					deathMoon = 1.0f;
					finaleMessage = "Tragedy";
				}
				else
				if(allNewRelationships)
				{
					Game.game.endGame();
					if(Game.game.soulWorld.deadones == 0 && depressed == 0)
					{
						happyMoon = 1.0f;
						deathMoon = 0;
						finaleMessage = "All Happiness";
					}
					else
					{
						happyMoon = 0.0f;
						deathMoon = 0.0f;
						regularMoon = 1.0f;
						finaleMessage = "Survivors Love";
					}
				}
			}
		}
		else
		{
			if(cutNow && !alreadyCut && fixed == null && canPlacemoon())
			{
				fixed = new Point(area().x,area().y);
			}
		}
	}
	
	private boolean canPlacemoon()
	{
		return !Game.game.city.hasBuilding(x, y) && y < (Game.game.city.getSkyLine() - moon.frameHeight/2);
	}
	
	public void setCutMode(boolean cutting)
	{
		alreadyCut = false;
		cutNow = cutting;
		if(!Game.game.isOver && cutting)
		{
			Sound.play("scissor.wav");
		}
	}
	
	public void paint(Graphics g,int px, int py)
	{
		if(!Game.game.isOver)
		{
			alienImg.paint(g, cutNow? 1 : 0, area().x, area().y);
		}
	}
	
	private void paintMoon(Graphics g)
	{
		int px = area().x;
		int py = area().y;
		if(fixed != null)
		{
			px = fixed.x;
			py = fixed.y;
		}
		Utils.pushAlpha(g, happyMoon);
		moon.paint(g, 0, px, py - moon.frameHeight/2);
		Utils.popAlpha(g);
		
		Utils.pushAlpha(g, deathMoon);
		moon.paint(g, 1, px, py - moon.frameHeight/2);
		Utils.popAlpha(g);

		Utils.pushAlpha(g, regularMoon);
		moon.paint(g, 2, px, py - moon.frameHeight/2);
		Utils.popAlpha(g);
	}

	public void paintOverlay(Graphics g, int px, int py)
	{
		if(Game.game.isOver)
		{
			if(fixed == null && !canPlacemoon())
			{
				Utils.pushAlpha(g, 0.45f);
				paintMoon(g);
				Utils.popAlpha(g);
			}
			else
			{
				paintMoon(g);
			}
			alienImg.paint(g, 3, area().x, area().y);
			return;
		}
		if(cutNow)
		{
			paint(g,px,py);
		}
		else
		{
			alienImg.paint(g, 2, area().x, area().y);
		}
	}
}
